<?php

namespace app\common\behavior;

use think\facade\Hook;

class Inithook
{
    public function run($content){
        $data = session('mor_hooks');
        if(!$data){
            $hooks = db('hooks')->where(['status'=>1])->field('name,addons')->select();
            if($hooks){
                foreach ($hooks as $key => $value) {
                    if($value['addons']){
                        $where[] = ['status','=','1'];
                        $names = explode(',',$value['addons']);
                        $where[] = ['name','in',$names];
                        $data = db('addons')->where($where)->field('name')->select();
                        if($data){
                            $addon_array = [];
                            foreach ($data as $v) {
                                $addon_array[] = $v['name'];
                            }
                            $addons = array_intersect($names,$addon_array);
                            Hook::add($value['name'],array_map('get_addons_class',$addons));
                        }
                    }
                }
                session('mor_hooks',Hook::get());
            }
        }else{
            Hook::import($data,false);
        }
    }
}